package com.yanze.schoolinformation.viewModule.login;

import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.req.ReqUser;
import com.yanze.schoolinformation.model.bean.resp.RespUser;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;
import com.yanze.schoolinformation.utils.BaseTools;
import com.yanze.schoolinformation.view.login_register.login.RegisterFragment;

import io.reactivex.disposables.Disposable;

/**
 * Created by MrZheng on 2018/5/7.
 */

public class RegisterViewModel {

    private RegisterFragment mFragment;
    private RegisterNavigator mNavigator;

    public RegisterViewModel(RegisterFragment mFragment) {
        this.mFragment = mFragment;
    }

    public void setmNavigator(RegisterNavigator mNavigator) {
        this.mNavigator = mNavigator;
    }

    /**
     * 注册
     */
    public void Register(String phone, String password) {
        ReqUser user = new ReqUser(phone, BaseTools.MD5(password));
        DataReqo.getInstant().register(C.url.API_School, user)
                .subscribe(new DeforObserver<RespUser>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        mFragment.addDisposable(disposable);
                    }

                    @Override
                    public void _onNext(RespUser entity) {
                        if (entity.getStatus() == 200) {
                            mNavigator.registerSuccess(entity);
                        }else{
                            mNavigator.registerFaild(entity.getMsg());
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        mNavigator.registerFaild(msg);
                    }
                });
    }

    public void onDestory() {
        this.mFragment = null;
        this.mNavigator = null;
    }
}
